package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xuanwei.core.entity.vo.FindContentVo;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwFindContentService;
import com.xuanwei.dao.entity.XwFindContent;
import com.xuanwei.dao.mapper.XwFindContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-08-07
 */
@Service
public class XwFindContentServiceImpl extends ServiceImpl<XwFindContentMapper, XwFindContent> implements IXwFindContentService {

    @Autowired
    private Environment environment;

    @Override
    public void add(Integer id, String content, String images, Integer orderBy) {
        if (id == null) {
            XwFindContent findContent = new XwFindContent();
            findContent.setUserAvatar(environment.getProperty("find.avatar"));
            findContent.setUserNickname(environment.getProperty("find.nickname"));
            findContent.setContent(content);
            findContent.setUserId(0);
            findContent.setImages(images);
            findContent.setOrderBy(orderBy);
            findContent.setCreateTime(LocalDateTime.now());
            findContent.setUpdateTime(LocalDateTime.now());
            save(findContent);
        } else {
            XwFindContent one = getOne(new QueryWrapper<XwFindContent>().select("id").eq("id", id));
            if (one == null) {
                throw new XwException("数据不存在");
            }
            one.setContent(content);
            one.setImages(images);
            one.setOrderBy(orderBy);
            one.setUpdateTime(LocalDateTime.now());
            updateById(one);
        }
    }

    @Override
    public void del(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void findPage(Page<XwFindContent> page) {

        page(page, new QueryWrapper<XwFindContent>().orderByAsc("order_by"));
    }

    @Override
    public List<FindContentVo> findList(Page<XwFindContent> page) {

        page(page, new QueryWrapper<XwFindContent>().orderByAsc("order_by").orderByDesc("create_time"));
        List<XwFindContent> records = page.getRecords();
        List<FindContentVo> findContentVos = Lists.newArrayListWithCapacity(records.size());
        records.forEach(s -> {
            FindContentVo findContentVo = new FindContentVo();
            findContentVo.setId(s.getId());
            findContentVo.setContent(s.getContent());
            findContentVo.setUserAvatar(s.getUserAvatar());
            findContentVo.setUserNickname(s.getUserNickname());
            findContentVo.setCreateTime(s.getCreateTime());
            findContentVo.setImages(Arrays.asList(s.getImages().split(",")));
            findContentVos.add(findContentVo);
        });

        return findContentVos;
    }
}
